#include<stdio.h>
#include"getch.h"
#include<stdlib.h>

int main(int argc,const char* argv[])
{
	//迷宫数组 墙壁=0 路=1 角色=2 
	int arr[10][10] = {
		{0,0,0,0,0,0,0,0,0,0},
		{0,1,0,0,0,0,0,0,1,0},
		{0,1,0,0,0,0,0,0,1,0},
		{0,2,0,0,0,0,0,0,1,1},
		{0,1,0,0,0,0,0,0,1,0},
		{0,1,1,1,1,1,1,0,1,0},
		{0,1,1,0,0,0,1,0,1,0},
		{0,1,1,0,0,0,1,1,1,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0}
	};

	int x = 3,y = 1;	// 角色当前坐标（根据迷宫地图初始化）
	int final_x = 3, final_y =9; 	// 终点坐标（根据迷宫地图初始化）
	
	//游戏开始
	while(1)
	{
		//清屏
		system("clear");
	
		//显示迷宫
		for(int i=0;i<10;i++)
		{
			for(int j=0;j<10;j++)
			{
				switch(arr[i][j])
				{
					case 0: printf("# ");break;
				case 1: printf("  ");break;
				case 2: printf("@ ");break;
				}
			}
			printf("\n");
		}

		//结束判断
		if(x == final_x && y == final_y)
		{
			printf("游戏结束\n");
			return 0;
		}
	
		//玩法实现
		switch(getch())
		{
			case 183:
				if(arr[x-1][y] == 1)
				{
					arr[x-1][y] = 2;
					arr[x][y] = 1;
					x--;
				}
				break;
			case 184:
				if(arr[x+1][y] == 1)
				{
					arr[x+1][y] = 2;
					arr[x][y] = 1;
					x++;
				}
				break;
			case 185:
				if(arr[x][y+1] == 1)
				{
					arr[x][y+1] = 2;
					arr[x][y] = 1;
					y++;
				}
				break;
			case 186:
				if(arr[x][y-1] == 1)
				{
					arr[x][y-1] = 2;
					arr[x][y] = 1;
					y--;
				}
				break;
		}
	}
}
